﻿Imports System.Collections.Generic
Imports Microsoft.Office.Interop.Outlook
Imports System.Data.Linq.SqlClient

Public Class pmMoveMailForm
    Private _app As Outlook.Application
    Private ReadOnly _mailToMove As MailItem

    Friend Sub New(ByVal App As Outlook.Application, ByVal mailToMove As MailItem)
        If IsNothing(App) Then
            Throw New ArgumentNullException("App")
        End If

        If IsNothing(mailToMove) Then
            Throw New ArgumentNullException("mailToMove")
        End If

        _app = App
        _mailToMove = mailToMove


        ' This call is required by the Windows Form Designer.
        InitializeComponent()

    End Sub


    Private Sub pmMoveMailForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DisplayFolderTree()
        SelectRelevantFolder()
    End Sub

    Private Sub SelectRelevantFolder()
        Dim folderSelecter As New RelevantFolderSelecter

        SelectFolderInTree(GetFolderByContactName(GetInboxFolder, _mailToMove.SenderEmailAddress))

    End Sub


    Private Sub SelectFolderInTree(ByVal folderToSelect As MAPIFolder)
        If folderToSelect Is Nothing Then
            Return
        End If
        For Each node As TreeNode In TreeView1.Nodes(0).Nodes
            If TryCast(node.Tag, MAPIFolder).FolderPath.Equals(folderToSelect.FolderPath) Then
                TreeView1.SelectedNode = node
            End If
        Next
    End Sub


    Private Function GetFolderByContactName(ByVal rootFolder As MAPIFolder, ByVal searchPattern As String) As MAPIFolder
        For Each folder As MAPIFolder In rootFolder.Folders
            If folder.Name.IndexOf(searchPattern) <> -1 Then
                Return folder
            End If
        Next
        Return Nothing
    End Function




    Private Function GetInboxFolder() As MAPIFolder
        Return _app.Application.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
    End Function


    Private Sub DisplayFolderTree()
        Dim inbox As Outlook.MAPIFolder = GetInboxFolder()

        Dim root As TreeNode = CreateTreeNode(inbox.Name, inbox, inbox.FolderPath)

        TreeView1.Nodes.Add(root)

        AddMailFoldersToTreeview(root, inbox)

        root.Expand()
    End Sub


    Private Sub AddMailFoldersToTreeview(ByVal parentNode As TreeNode, ByVal rootFolder As Outlook.MAPIFolder)

        Dim sorted = From n As MAPIFolder In rootFolder.Folders.OfType(Of MAPIFolder)() _
            Order By n.Name _
            Select n

        For Each folder As Outlook.MAPIFolder In sorted
            If folder.DefaultItemType = Outlook.OlItemType.olMailItem Then
                Dim childNode As TreeNode = CreateTreeNode(folder.Name, folder, folder.FolderPath)

                parentNode.Nodes.Add(childNode)
                AddMailFoldersToTreeview(childNode, folder)
            End If
        Next
    End Sub


    Private Function CreateTreeNode(ByVal text As String, ByVal tag As MAPIFolder, ByVal name As String) As TreeNode
        Dim node As New TreeNode(text)
        node.Tag = tag
        node.Name = name
        Return node
    End Function


    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        CloseWindow()
    End Sub

    Private Sub btnMove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMove.Click
        Try
            MoveMail()
            CloseWindow()
        Catch ex As InvalidOperationException
            MessageBox.Show(ex.Message)
        End Try
    End Sub


    Private Sub MoveMail()
        Dim destinationFolder As Outlook.MAPIFolder = GetSelectedFolder()

        If IsNothing(destinationFolder) OrElse destinationFolder.FolderPath = TryCast(_mailToMove.Parent, MAPIFolder).FolderPath Then
            Return
        End If

        _mailToMove.Move(destinationFolder)
    End Sub

    Private Function GetSelectedFolder() As MAPIFolder
        If IsNothing(TreeView1.SelectedNode) Then
            Throw New InvalidOperationException("Gelieve een folder te selecteren")
        End If
        Return DirectCast(TreeView1.SelectedNode.Tag, MAPIFolder)
    End Function

    Private Sub CloseWindow()
        Me.Close()
    End Sub
End Class

Public Class RelevantFolderSelecter

End Class